﻿using System;
using System.Windows;
using System.Windows.Controls;
using Croxweb.Leuphana.ViewModels;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Tasks;

namespace Croxweb.Leuphana.Pages
{
    public partial class MainPage : PhoneApplicationPage
    {
        private MainViewModel viewModel;

        // Constructor.
        public MainPage()
        {
            InitializeComponent();

            // Datacontext to view model.
            viewModel = new MainViewModel(App.MyStudyService, App.Config);
            DataContext = viewModel;
        }

        // Load view model data.
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (!viewModel.IsDataLoaded)
            {
                viewModel.Refresh();
            }

            base.OnNavigatedTo(e);
        }

        #region Menu Events

        private void Button_Persons(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/PersonSearchPage.xaml", UriKind.Relative));
        }        
        
        private void Button_Messages(object sender, RoutedEventArgs e)
        {
        }

        private void Button_Schedule(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/SchedulePage.xaml", UriKind.Relative));
        }

        private void Button_Settings(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Pages/SettingsPage.xaml", UriKind.Relative));
        }

        private void Button_Logout(object sender, RoutedEventArgs e)
        {
            viewModel.Logout();
        }

        #endregion

        #region Selection Events

        private void Appointments_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ListBox listBox = sender as ListBox;
            if (listBox != null)
            {
                AppointmentViewModel avm = listBox.SelectedItem as AppointmentViewModel;
                if (avm != null)
                {
                    NavigationService.Navigate(new Uri("/Pages/LecturePage.xaml?id=" + avm.LectureId, UriKind.Relative));
                    ((ListBox)sender).SelectedIndex = -1;
                }
            }
        }

        private void Messages_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MessageViewModel message = ((ListBox)sender).SelectedItem as MessageViewModel;
            if (null != message)
            {
                NavigationService.Navigate(new Uri("/Pages/MessagePage.xaml?id=" + message.Id, UriKind.Relative));
                ((ListBox)sender).SelectedIndex = -1;
            }
        }

        private void Panorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!viewModel.IsUpdated && ((Panorama)sender).SelectedIndex == 2)
            {
                viewModel.SyncMessages();
            }
        }

        #endregion

        #region Login Events

        private void MyStudyLogin_Login(object sender, Controls.LoginEventArgs e)
        {
            viewModel.Login(e.Username, e.Password);
        }

        private void MyStudyLogin_ShowCertClick(object sender, RoutedEventArgs e)
        {
            WebBrowserTask browser = new WebBrowserTask();
            browser.URL = "http://croxweb.de/ca.zip";
            browser.Show();
        }

        private void MyStudyLogin_ShowPortalClick(object sender, RoutedEventArgs e)
        {
            WebBrowserTask browser = new WebBrowserTask();
            browser.URL = "https://mystudy.leuphana.de/portal/home";
            browser.Show();
        }
        
        #endregion
    }
}